*************************************************************************
* Created by: 	Rob Valletta						
* Created on: 	05/19/2022
* Description: 	List tabulations of UI_rep distribution by occ and ind
* Modified on:	Jan 11th, 2023 (Mary Y.)
*		12/22/23 (RV): Do black/white, fix "lfstat" restriction
*		for single match sample (previously had "lfstat2==1")
*		5/22/24: Do "eps" version of charts for pub version (no title or notes)
*		7/10/24: Enlarge label sizes on charts ("-l" versions)
*************************************************************************

version 15
clear *
set more off
capture program drop _all
cap log close


set linesize 240

local sdrive 0 

if `sdrive' local projdir "S:/RobV/UI_2020"
else local projdir "C:/data/UI_2020"


log using "`projdir'/logs/table_UI-rep_ind-occv2-b2_$S_DATE", replace


** Pull in analysis extract 
use `projdir'/data/CPS_2020_unbalanced_v2.dta, clear


* FORM UI REPLACEMENT RATE (WITH MISSINGS), DROP 0'S and DC
gen UI_rep=(UI_CARES/weekly_earnings)
  replace UI_rep=0 if UI_rep==.
drop if UI_rep==0
drop if st=="DC"

egen double pid=group(peridnum)
xtset pid mis
gen pwlgwgt_b=f.pwlgwgt if matchf==1
  replace pwlgwgt=pwlgwgt_b

** FORM CORRECTED MAJOCC
gen majocc2=majocc
  replace majocc2=l.majocc2 if (majocc<0 & lfstat2==2 & l.lfstat2==2)

** SET NEG MAJIND AND MAJOCC TO MISSING
replace majind=. if majind<=0
replace majind2=. if majind2<=0
replace majocc=. if majocc<=0
replace majocc2=. if majocc2<=0

** Label majind and majocc variables
#d ;
label define ind 1 "Ag" 2 "Mining" 3 "Const" 4 "Manufac" 5 "Whl_Ret" 6 "Trans_util" 7 "Info" 
8 "Financial" 9 "Prof_Bus" 10 "Educ_Health" 11 "Leis_Hosp" 12 "Oth_Serv" 13 "Gov" 14 "Armed_Forces";
#d cr
label values majind ind
label values majind2 ind
label variable majind "Major industry recode"
label variable majind2 "Majind adjusted for lfstat2"

#d ;
label define occ 1 "Manag" 2 "Prof_tech" 3 "Services" 4 "Sales" 5 "Admin" 6 "Farm_Fish"
7 "Const_Extract" 8 "Inst_Maint_Rep" 9 "Production" 10 "Trans_Materials" 11 "Armed_Forces";
#d cr
label values majocc occ
label values majocc2 occ
label variable majocc "Major occupation recode"
label variable majocc2 "Majocc adjusted for lfstat2"


** DROP FLAGGED BAD MATCHES
drop if fl_bad_cps==1

**KEEP APRIL - JULY
keep if month >= 4 & month <= 7


****ADDED IN 2023: MAKE BOX-AND-WHISKER PLOTS****

/*Loop through and make these graphs for (i) U==1 observations (without any match restriction),
and (ii) after imposing (i), restrict to the double match sample ("match2f==1"). */

foreach sample in 1 2 {
    
	preserve
		
	if `sample' == 1 {
	    keep if lfstat==2
		local weight wgtbls
		local name "nomatch"
	} 
	else {
	    keep if lfstat2==2 & match2f==1
		local weight pwlgwgt
		local name "match2f"
		
	}
	
	
	*Full sample
	qui sum UI_rep [aw = `weight'], de 

	gen full_mean = r(mean)
	gen full_sd = r(sd)
	gen full_median = r(p50)

	tempfile preserve2
	save `preserve2', replace
	
	foreach type in majocc majind {
		
		use `preserve2', clear
		
		collapse full_mean full_sd full_median ///
		(mean) mean_UI_rep=UI_rep (median) med_UI_rep=UI_rep (sd) ///
		sd_UI_rep=UI_rep [aw = `weight'], by(`type')
		
		drop if `type' == .
		expand 2 in 1
		
		replace mean_UI_re = full_mean if _n == _N
		replace sd_UI_rep = full_sd if _n == _N
		replace med_UI_rep = full_median if _n == _N
		replace `type' = 0 if _n == _N
		
		drop full_mean full_sd full_median
		
		*high/low value, +1SD, -1SD
		
		gen high = mean_UI_re + sd_UI_rep
		gen low = mean_UI_re - sd_UI_rep
		
		
		if "`type'" == "majocc"{
			
			drop if `type' == 11  //armed forces
		
			gr tw (rcap high low `type', lcolor(black) lp(dash)) ///
			(scatter mean_UI_rep `type',mcolor(black)) ///
			(scatter med_UI_rep `type', mcolor(gray) m(T)), ///
			graphregion(color(white)) ///
			legend(order(2 "Mean" 3 "Median") rows(1) size(small)  ring(0) position(12) region(lcolor(black))) ///
			xtitle("") ///
			xlabel(0 "Full sample" 1 "Man/business/fin" 2 "Professional" ///
			3 "Services" 4 "Sales" 5 "Office/admin" 6 "Farm/fish/for" ///
			7 "Constr/extract" 8 "Inst/maint/rep" 9 "Production" 10 "Trans/material" ///
			11 " ", labsize(small) alternate nogrid) ///
			ylab(,nogrid)


			gr_edit .xaxis1.major.num_rule_ticks = 0
			gr_edit .xaxis1.edit_tick 12 11 `" "', custom tickset(major) editstyle(tickstyle(show_ticks(no)) )
			// xaxis1 edits

			graph export `projdir'/output/UI_rep_by_occ_`name'-bw-l.eps, replace
			graph export `projdir'/output/UI_rep_by_occ_`name'-bw-l.png, replace
		}
		
		else {
			
			drop if `type' == 14
			
			gr tw (rcap high low `type', lp(dash) lc(black)) ///
			(scatter mean_UI_re `type', mcolor(black)) ///
			(scatter med_UI_rep `type', mcolor(gray) m(T)), ///
			graphregion(color(white)) ///
			xtitle("") ///
			legend(order(2 "Mean" 3 "Median") rows(1) size(small)  ring(0) position(12) region(lcolor(black))) ///
			xlabel(0 "Full sample" 1 "Ag/for/fish" 2 "Mining" ///
			3 "Construction" 4 "Manufacturing" 5 "Whole/retail" 6 "Trans/util" ///
			7 "Information" 8 "Finance" 9 "Prof/bus serv." 10 "Educ/health" ///
			11 "Leis/hosp" 12 "Other serv." 13 "Government" 14 " ", labsize(small) alternate nogrid) ///
			ylab(,nogrid)
			
			gr_edit .xaxis1.major.num_rule_ticks = 0
			gr_edit .xaxis1.edit_tick 15 14 `" "', custom tickset(major) editstyle(tickstyle(show_ticks(no)) )
			
			graph export `projdir'/output/UI_rep_by_ind_`name'-bw-l.eps, replace
			graph export `projdir'/output/UI_rep_by_ind_`name'-bw-l.png, replace

		}
		
		
		
	}
	
	restore
}

log close












